home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / stl / ropeimpl.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-30  |  48.9 KB  |  1,588 lines

  1. /*
  2.  * Copyright (c) 1997
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Permission to use, copy, modify, distribute and sell this software
  6.  * and its documentation for any purpose is hereby granted without fee,
  7.  * provided that the above copyright notice appear in all copies and
  8.  * that both that copyright notice and this permission notice appear
  9.  * in supporting documentation.  Silicon Graphics makes no
  10.  * representations about the suitability of this software for any
  11.  * purpose.  It is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. /* NOTE: This is an internal header file, included by other STL headers.
  15.  *   You should not attempt to use it directly.
  16.  */
  17.  
  18. # include <stdio.h>     
  19.  
  20. #ifdef __STL_USE_NEW_IOSTREAMS 
  21. # include <iostream>
  22. #else /* __STL_USE_NEW_IOSTREAMS */
  23. # include <iostream.h>
  24. #endif /* __STL_USE_NEW_IOSTREAMS */
  25.  
  26. #ifdef __STL_USE_EXCEPTIONS
  27. # include <stdexcept>
  28. #endif
  29.  
  30. __STL_BEGIN_NAMESPACE
  31.  
  32. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  33. #pragma set woff 1174
  34. #endif
  35.  
  36. // Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf
  37. // if necessary.  Assumes _M_path_end[leaf_index] and leaf_pos are correct.
  38. // Results in a valid buf_ptr if the iterator can be legitimately
  39. // dereferenced.
  40. template <class _CharT, class _Alloc>
  41. void _Rope_iterator_base<_CharT,_Alloc>::_S_setbuf( 
  42.   _Rope_iterator_base<_CharT,_Alloc>& __x)
  43. {
  44.     const _RopeRep* __leaf = __x._M_path_end[__x._M_leaf_index];
  45.     size_t __leaf_pos = __x._M_leaf_pos;
  46.     size_t __pos = __x._M_current_pos;
  47.  
  48.     switch(__leaf->_M_tag) {
  49.     case _RopeRep::_S_leaf:
  50.         __x._M_buf_start = 
  51.           ((_Rope_RopeLeaf<_CharT,_Alloc>*)__leaf)->_M_data;
  52.         __x._M_buf_ptr = __x._M_buf_start + (__pos - __leaf_pos);
  53.         __x._M_buf_end = __x._M_buf_start + __leaf->_M_size;
  54.         break;
  55.     case _RopeRep::_S_function:
  56.     case _RopeRep::_S_substringfn:
  57.         {
  58.         size_t __len = _S_iterator_buf_len;
  59.         size_t __buf_start_pos = __leaf_pos;
  60.         size_t __leaf_end = __leaf_pos + __leaf->_M_size;
  61.         char_producer<_CharT>* __fn =
  62.             ((_Rope_RopeFunction<_CharT,_Alloc>*)__leaf)->_M_fn;
  63.  
  64.         if (__buf_start_pos + __len <= __pos) {
  65.             __buf_start_pos = __pos - __len/4;
  66.             if (__buf_start_pos + __len > __leaf_end) {
  67.             __buf_start_pos = __leaf_end - __len;
  68.             }
  69.         }
  70.         if (__buf_start_pos + __len > __leaf_end) {
  71.             __len = __leaf_end - __buf_start_pos;
  72.         }
  73.         (*__fn)(__buf_start_pos - __leaf_pos, __len, __x._M_tmp_buf);
  74.         __x._M_buf_ptr = __x._M_tmp_buf + (__pos - __buf_start_pos);
  75.         __x._M_buf_start = __x._M_tmp_buf;
  76.         __x._M_buf_end = __x._M_tmp_buf + __len;
  77.         }
  78.         break;
  79.     default:
  80.         __stl_assert(0);
  81.     }
  82. }
  83.  
  84. // Set path and buffer inside a rope iterator.  We assume that 
  85. // pos and root are already set.
  86. template <class _CharT, class _Alloc>
  87. void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache
  88. (_Rope_iterator_base<_CharT,_Alloc>& __x)
  89. {
  90.     const _RopeRep* __path[_RopeRep::_S_max_rope_depth+1];
  91.     const _RopeRep* __curr_rope;
  92.     int __curr_depth = -1;  /* index into path    */
  93.     size_t __curr_start_pos = 0;
  94.     size_t __pos = __x._M_current_pos;
  95.     unsigned char __dirns = 0; // Bit vector marking right turns in the path
  96.  
  97.     __stl_assert(__pos <= __x._M_root->_M_size);
  98.     if (__pos >= __x._M_root->_M_size) {
  99.     __x._M_buf_ptr = 0;
  100.     return;
  101.     }
  102.     __curr_rope = __x._M_root;
  103.     if (0 != __curr_rope->_M_c_string) {
  104.     /* Treat the root as a leaf. */
  105.     __x._M_buf_start = __curr_rope->_M_c_string;
  106.     __x._M_buf_end = __curr_rope->_M_c_string + __curr_rope->_M_size;
  107.     __x._M_buf_ptr = __curr_rope->_M_c_string + __pos;
  108.     __x._M_path_end[0] = __curr_rope;
  109.     __x._M_leaf_index = 0;
  110.     __x._M_leaf_pos = 0;
  111.     return;
  112.     }
  113.     for(;;) {
  114.     ++__curr_depth;
  115.     __stl_assert(__curr_depth <= _RopeRep::_S_max_rope_depth);
  116.     __path[__curr_depth] = __curr_rope;
  117.     switch(__curr_rope->_M_tag) {
  118.       case _RopeRep::_S_leaf:
  119.       case _RopeRep::_S_function:
  120.       case _RopeRep::_S_substringfn:
  121.         __x._M_leaf_pos = __curr_start_pos;
  122.         goto done;
  123.       case _RopeRep::_S_concat:
  124.         {
  125.         _Rope_RopeConcatenation<_CharT,_Alloc>* __c =
  126.             (_Rope_RopeConcatenation<_CharT,_Alloc>*)__curr_rope;
  127.         _RopeRep* __left = __c->_M_left;
  128.         size_t __left_len = __left->_M_size;
  129.         
  130.         __dirns <<= 1;
  131.         if (__pos >= __curr_start_pos + __left_len) {
  132.             __dirns |= 1;
  133.             __curr_rope = __c->_M_right;
  134.             __curr_start_pos += __left_len;
  135.         } else {
  136.             __curr_rope = __left;
  137.         }
  138.         }
  139.         break;
  140.     }
  141.     }
  142.   done:
  143.     // Copy last section of path into _M_path_end.
  144.       {
  145.     int __i = -1;
  146.     int __j = __curr_depth + 1 - _S_path_cache_len;
  147.  
  148.     if (__j < 0) __j = 0;
  149.     while (__j <= __curr_depth) {
  150.         __x._M_path_end[++__i] = __path[__j++];
  151.     }
  152.     __x._M_leaf_index = __i;
  153.       }
  154.       __x._M_path_directions = __dirns;
  155.       _S_setbuf(__x);
  156. }
  157.  
  158. // Specialized version of the above.  Assumes that
  159. // the path cache is valid for the previous position.
  160. template <class _CharT, class _Alloc>
  161. void _Rope_iterator_base<_CharT,_Alloc>::_S_setcache_for_incr
  162. (_Rope_iterator_base<_CharT,_Alloc>& __x)
  163. {
  164.     int __current_index = __x._M_leaf_index;
  165.     const _RopeRep* __current_node = __x._M_path_end[__current_index];
  166.     size_t __len = __current_node->_M_size;
  167.     size_t __node_start_pos = __x._M_leaf_pos;
  168.     unsigned char __dirns = __x._M_path_directions;
  169.     _Rope_RopeConcatenation<_CharT,_Alloc>* __c;
  170.  
  171.     __stl_assert(__x._M_current_pos <= __x._M_root->_M_size);
  172.     if (__x._M_current_pos - __node_start_pos < __len) {
  173.     /* More stuff in this leaf, we just didn't cache it. */
  174.     _S_setbuf(__x);
  175.     return;
  176.     }
  177.     __stl_assert(__node_start_pos + __len == __x._M_current_pos);
  178.     //  node_start_pos is starting position of last_node.
  179.     while (--__current_index >= 0) {
  180.     if (!(__dirns & 1) /* Path turned left */) 
  181.       break;
  182.     __current_node = __x._M_path_end[__current_index];
  183.     __c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node;
  184.     // Otherwise we were in the right child.  Thus we should pop
  185.     // the concatenation node.
  186.     __node_start_pos -= __c->_M_left->_M_size;
  187.     __dirns >>= 1;
  188.     }
  189.     if (__current_index < 0) {
  190.     // We underflowed the cache. Punt.
  191.     _S_setcache(__x);
  192.     return;
  193.     }
  194.     __current_node = __x._M_path_end[__current_index];
  195.     __c = (_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node;
  196.     // current_node is a concatenation node.  We are positioned on the first
  197.     // character in its right child.
  198.     // node_start_pos is starting position of current_node.
  199.     __node_start_pos += __c->_M_left->_M_size;
  200.     __current_node = __c->_M_right;
  201.     __x._M_path_end[++__current_index] = __current_node;
  202.     __dirns |= 1;
  203.     while (_RopeRep::_S_concat == __current_node->_M_tag) {
  204.     ++__current_index;
  205.     if (_S_path_cache_len == __current_index) {
  206.         int __i;
  207.         for (__i = 0; __i < _S_path_cache_len-1; __i++) {
  208.         __x._M_path_end[__i] = __x._M_path_end[__i+1];
  209.         }
  210.         --__current_index;
  211.     }
  212.     __current_node =
  213.         ((_Rope_RopeConcatenation<_CharT,_Alloc>*)__current_node)->_M_left;
  214.     __x._M_path_end[__current_index] = __current_node;
  215.     __dirns <<= 1;
  216.     // node_start_pos is unchanged.
  217.     }
  218.     __x._M_leaf_index = __current_index;
  219.     __x._M_leaf_pos = __node_start_pos;
  220.     __x._M_path_directions = __dirns;
  221.     _S_setbuf(__x);
  222. }
  223.  
  224. template <class _CharT, class _Alloc>
  225. void _Rope_iterator_base<_CharT,_Alloc>::_M_incr(size_t __n) {
  226.     _M_current_pos += __n;
  227.     if (0 != _M_buf_ptr) {
  228.         size_t __chars_left = _M_buf_end - _M_buf_ptr;
  229.         if (__chars_left > __n) {
  230.             _M_buf_ptr += __n;
  231.         } else if (__chars_left == __n) {
  232.             _M_buf_ptr += __n;
  233.             _S_setcache_for_incr(*this);
  234.         } else {
  235.             _M_buf_ptr = 0;
  236.         }
  237.     }
  238. }
  239.  
  240. template <class _CharT, class _Alloc>
  241. void _Rope_iterator_base<_CharT,_Alloc>::_M_decr(size_t __n) {
  242.     if (0 != _M_buf_ptr) {
  243.         size_t __chars_left = _M_buf_ptr - _M_buf_start;
  244.         if (__chars_left >= __n) {
  245.             _M_buf_ptr -= __n;
  246.         } else {
  247.             _M_buf_ptr = 0;
  248.         }
  249.     }
  250.     _M_current_pos -= __n;
  251. }
  252.  
  253. template <class _CharT, class _Alloc>
  254. void _Rope_iterator<_CharT,_Alloc>::_M_check() {
  255.     if (_M_root_rope->_M_tree_ptr != _M_root) {
  256.         // _Rope was modified.  Get things fixed up.
  257.         _RopeRep::_S_unref(_M_root);
  258.         _M_root = _M_root_rope->_M_tree_ptr;
  259.         _RopeRep::_S_ref(_M_root);
  260.         _M_buf_ptr = 0;
  261.     }
  262. }
  263.  
  264. template <class _CharT, class _Alloc>
  265. inline 
  266. _Rope_const_iterator<_CharT, _Alloc>::_Rope_const_iterator(
  267.   const _Rope_iterator<_CharT,_Alloc>& __x)
  268. : _Rope_iterator_base<_CharT,_Alloc>(__x) 
  269. { }
  270.  
  271. template <class _CharT, class _Alloc>
  272. inline _Rope_iterator<_CharT,_Alloc>::_Rope_iterator(
  273.   rope<_CharT,_Alloc>& __r, size_t __pos)
  274. : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos), 
  275.   _M_root_rope(&__r)
  276. {
  277.     _RopeRep::_S_ref(_M_root);
  278. }
  279.  
  280. template <class _CharT, class _Alloc>
  281. inline size_t 
  282. rope<_CharT,_Alloc>::_S_char_ptr_len(const _CharT* __s)
  283. {
  284.     const _CharT* __p = __s;
  285.  
  286.     while (!_S_is0(*__p)) { ++__p; }
  287.     return (__p - __s);
  288. }
  289.  
  290.  
  291. #ifndef __GC
  292.  
  293. template <class _CharT, class _Alloc>
  294. inline void _Rope_RopeRep<_CharT,_Alloc>::_M_free_c_string()
  295. {
  296.     _CharT* __cstr = _M_c_string;
  297.     if (0 != __cstr) {
  298.     size_t __size = _M_size + 1;
  299.     destroy(__cstr, __cstr + __size);
  300.     _Data_deallocate(__cstr, __size);
  301.     }
  302. }
  303.  
  304.  
  305. template <class _CharT, class _Alloc>
  306. #ifdef __STL_USE_STD_ALLOCATORS
  307.   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s,
  308.                                size_t __n,
  309.                                    allocator_type __a)
  310. #else
  311.   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string(_CharT* __s,
  312.                                size_t __n)
  313. #endif
  314. {
  315.     if (!_S_is_basic_char_type((_CharT*)0)) {
  316.     destroy(__s, __s + __n);
  317.     }
  318. //  This has to be a static member, so this gets a bit messy
  319. #   ifdef __STL_USE_STD_ALLOCATORS
  320.         __a.deallocate(
  321.         __s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n));
  322. #   else
  323.     _Data_deallocate(
  324.         __s, _Rope_RopeLeaf<_CharT,_Alloc>::_S_rounded_up_size(__n));
  325. #   endif
  326. }
  327.  
  328.  
  329. //  There are several reasons for not doing this with virtual destructors
  330. //  and a class specific delete operator:
  331. //  - A class specific delete operator can't easily get access to
  332. //    allocator instances if we need them.
  333. //  - Any virtual function would need a 4 or byte vtable pointer;
  334. //    this only requires a one byte tag per object.
  335. template <class _CharT, class _Alloc>
  336. void _Rope_RopeRep<_CharT,_Alloc>::_M_free_tree()
  337. {
  338.     switch(_M_tag) {
  339.     case _S_leaf:
  340.         {
  341.             _Rope_RopeLeaf<_CharT,_Alloc>* __l
  342.             = (_Rope_RopeLeaf<_CharT,_Alloc>*)this;
  343.             __l->_Rope_RopeLeaf<_CharT,_Alloc>::~_Rope_RopeLeaf();
  344.             _L_deallocate(__l, 1);
  345.             break;
  346.         }
  347.     case _S_concat:
  348.         {
  349.             _Rope_RopeConcatenation<_CharT,_Alloc>* __c
  350.             = (_Rope_RopeConcatenation<_CharT,_Alloc>*)this;
  351.             __c->_Rope_RopeConcatenation<_CharT,_Alloc>::
  352.                ~_Rope_RopeConcatenation();
  353.             _C_deallocate(__c, 1);
  354.             break;
  355.         }
  356.     case _S_function:
  357.         {
  358.             _Rope_RopeFunction<_CharT,_Alloc>* __f
  359.             = (_Rope_RopeFunction<_CharT,_Alloc>*)this;
  360.             __f->_Rope_RopeFunction<_CharT,_Alloc>::~_Rope_RopeFunction();
  361.             _F_deallocate(__f, 1);
  362.             break;
  363.         }
  364.     case _S_substringfn:
  365.         {
  366.             _Rope_RopeSubstring<_CharT,_Alloc>* __stl__ss =
  367.             (_Rope_RopeSubstring<_CharT,_Alloc>*)this;
  368.         __stl__ss->_Rope_RopeSubstring<_CharT,_Alloc>::
  369.                 ~_Rope_RopeSubstring();
  370.         _S_deallocate(__stl__ss, 1);
  371.         break;
  372.         }
  373.     }
  374. }
  375. #else
  376.  
  377. template <class _CharT, class _Alloc>
  378. #ifdef __STL_USE_STD_ALLOCATORS
  379.   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string
  380.         (const _CharT*, size_t, allocator_type)
  381. #else
  382.   inline void _Rope_RopeRep<_CharT,_Alloc>::_S_free_string
  383.         (const _CharT*, size_t)
  384. #endif
  385. {}
  386.  
  387. #endif
  388.  
  389.  
  390. // Concatenate a C string onto a leaf rope by copying the rope data.
  391. // Used for short ropes.
  392. template <class _CharT, class _Alloc>
  393. rope<_CharT,_Alloc>::_RopeLeaf*
  394. rope<_CharT,_Alloc>::_S_leaf_concat_char_iter
  395.         (_RopeLeaf* __r, const _CharT* __iter, size_t __len)
  396. {
  397.     size_t __old_len = __r->_M_size;
  398.     _CharT* __new_data = (_CharT*)
  399.     _Data_allocate(_S_rounded_up_size(__old_len + __len));
  400.     _RopeLeaf* __result;
  401.     
  402.     uninitialized_copy_n(__r->_M_data, __old_len, __new_data);
  403.     uninitialized_copy_n(__iter, __len, __new_data + __old_len);
  404.     _S_cond_store_eos(__new_data[__old_len + __len]);
  405.     __STL_TRY {
  406.     __result = _S_new_RopeLeaf(__new_data, __old_len + __len,
  407.                    __r->get_allocator());
  408.     }
  409.     __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len,
  410.                          __r->get_allocator()));
  411.     return __result;
  412. }
  413.  
  414. #ifndef __GC
  415. // As above, but it's OK to clobber original if refcount is 1
  416. template <class _CharT, class _Alloc>
  417. rope<_CharT,_Alloc>::_RopeLeaf*
  418. rope<_CharT,_Alloc>::_S_destr_leaf_concat_char_iter
  419.         (_RopeLeaf* __r, const _CharT* __iter, size_t __len)
  420. {
  421.     __stl_assert(__r->_M_ref_count >= 1);
  422.     if (__r->_M_ref_count > 1)
  423.       return _S_leaf_concat_char_iter(__r, __iter, __len);
  424.     size_t __old_len = __r->_M_size;
  425.     if (_S_allocated_capacity(__old_len) >= __old_len + __len) {
  426.     // The space has been partially initialized for the standard
  427.     // character types.  But that doesn't matter for those types.
  428.     uninitialized_copy_n(__iter, __len, __r->_M_data + __old_len);
  429.     if (_S_is_basic_char_type((_CharT*)0)) {
  430.         _S_cond_store_eos(__r->_M_data[__old_len + __len]);
  431.         __stl_assert(__r->_M_c_string == __r->_M_data);
  432.     } else if (__r->_M_c_string != __r->_M_data && 0 != __r->_M_c_string) {
  433.         __r->_M_free_c_string();
  434.         __r->_M_c_string = 0;
  435.     }
  436.     __r->_M_size = __old_len + __len;
  437.     __stl_assert(__r->_M_ref_count == 1);
  438.     __r->_M_ref_count = 2;
  439.     return __r;
  440.     } else {
  441.     _RopeLeaf* __result = _S_leaf_concat_char_iter(__r, __iter, __len);
  442.     __stl_assert(__result->_M_ref_count == 1);
  443.     return __result;
  444.     }
  445. }
  446. #endif
  447.  
  448. // Assumes left and right are not 0.
  449. // Does not increment (nor decrement on exception) child reference counts.
  450. // Result has ref count 1.
  451. template <class _CharT, class _Alloc>
  452. rope<_CharT,_Alloc>::_RopeRep*
  453. rope<_CharT,_Alloc>::_S_tree_concat (_RopeRep* __left, _RopeRep* __right)
  454. {
  455.     _RopeConcatenation* __result =
  456.       _S_new_RopeConcatenation(__left, __right, __left->get_allocator());
  457.     size_t __depth = __result->_M_depth;
  458.     
  459. #   ifdef __STL_USE_STD_ALLOCATORS
  460.       __stl_assert(__left->get_allocator() == __right->get_allocator());
  461. #   endif
  462.     if (__depth > 20 && (__result->_M_size < 1000 ||
  463.              __depth > _RopeRep::_S_max_rope_depth)) {
  464.         _RopeRep* __balanced;
  465.       
  466.     __STL_TRY {
  467.        __balanced = _S_balance(__result);
  468. #          ifndef __GC
  469.          if (__result != __balanced) {
  470.         __stl_assert(1 == __result->_M_ref_count
  471.                  && 1 == __balanced->_M_ref_count);
  472.          }
  473. #          endif
  474.        __result->_M_unref_nonnil();
  475.         }
  476.     __STL_UNWIND((_C_deallocate(__result,1)));
  477.         // In case of exception, we need to deallocate
  478.         // otherwise dangling result node.  But caller
  479.         // still owns its children.  Thus unref is
  480.         // inappropriate.
  481.     return __balanced;
  482.     } else {
  483.     return __result;
  484.     }
  485. }
  486.  
  487. template <class _CharT, class _Alloc>
  488. rope<_CharT,_Alloc>::_RopeRep* rope<_CharT,_Alloc>::_S_concat_char_iter
  489.         (_RopeRep* __r, const _CharT*__s, size_t __slen)
  490. {
  491.     _RopeRep* __result;
  492.     if (0 == __slen) {
  493.     _S_ref(__r);
  494.     return __r;
  495.     }
  496.     if (0 == __r)
  497.       return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
  498.                           __r->get_allocator());
  499.     if (_RopeRep::_S_leaf == __r->_M_tag && 
  500.           __r->_M_size + __slen <= _S_copy_max) {
  501.     __result = _S_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
  502. #       ifndef __GC
  503.       __stl_assert(1 == __result->_M_ref_count);
  504. #       endif
  505.     return __result;
  506.     }
  507.     if (_RopeRep::_S_concat == __r->_M_tag
  508.     && _RopeRep::_S_leaf == ((_RopeConcatenation*)__r)->_M_right->_M_tag) {
  509.     _RopeLeaf* __right = 
  510.       (_RopeLeaf* )(((_RopeConcatenation* )__r)->_M_right);
  511.     if (__right->_M_size + __slen <= _S_copy_max) {
  512.       _RopeRep* __left = ((_RopeConcatenation*)__r)->_M_left;
  513.       _RopeRep* __nright = 
  514.         _S_leaf_concat_char_iter((_RopeLeaf*)__right, __s, __slen);
  515.       __left->_M_ref_nonnil();
  516.       __STL_TRY {
  517.         __result = _S_tree_concat(__left, __nright);
  518.           }
  519.       __STL_UNWIND(_S_unref(__left); _S_unref(__nright));
  520. #         ifndef __GC
  521.         __stl_assert(1 == __result->_M_ref_count);
  522. #         endif
  523.       return __result;
  524.     }
  525.     }
  526.     _RopeRep* __nright =
  527.       __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
  528.     __STL_TRY {
  529.       __r->_M_ref_nonnil();
  530.       __result = _S_tree_concat(__r, __nright);
  531.     }
  532.     __STL_UNWIND(_S_unref(__r); _S_unref(__nright));
  533. #   ifndef __GC
  534.       __stl_assert(1 == __result->_M_ref_count);
  535. #   endif
  536.     return __result;
  537. }
  538.  
  539. #ifndef __GC
  540. template <class _CharT, class _Alloc>
  541. rope<_CharT,_Alloc>::_RopeRep* 
  542. rope<_CharT,_Alloc>::_S_destr_concat_char_iter(
  543.   _RopeRep* __r, const _CharT* __s, size_t __slen)
  544. {
  545.     _RopeRep* __result;
  546.     if (0 == __r)
  547.       return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
  548.                           __r->get_allocator());
  549.     size_t __count = __r->_M_ref_count;
  550.     size_t __orig_size = __r->_M_size;
  551.     __stl_assert(__count >= 1);
  552.     if (__count > 1) return _S_concat_char_iter(__r, __s, __slen);
  553.     if (0 == __slen) {
  554.     __r->_M_ref_count = 2;      // One more than before
  555.     return __r;
  556.     }
  557.     if (__orig_size + __slen <= _S_copy_max && 
  558.           _RopeRep::_S_leaf == __r->_M_tag) {
  559.     __result = _S_destr_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
  560.     return __result;
  561.     }
  562.     if (_RopeRep::_S_concat == __r->_M_tag) {
  563.     _RopeLeaf* __right = (_RopeLeaf*)(((_RopeConcatenation*)__r)->_M_right);
  564.     if (_RopeRep::_S_leaf == __right->_M_tag
  565.         && __right->_M_size + __slen <= _S_copy_max) {
  566.       _RopeRep* __new_right = 
  567.         _S_destr_leaf_concat_char_iter(__right, __s, __slen);
  568.       if (__right == __new_right) {
  569.           __stl_assert(__new_right->_M_ref_count == 2);
  570.           __new_right->_M_ref_count = 1;
  571.       } else {
  572.           __stl_assert(__new_right->_M_ref_count >= 1);
  573.           __right->_M_unref_nonnil();
  574.       }
  575.       __stl_assert(__r->_M_ref_count == 1);
  576.       __r->_M_ref_count = 2;    // One more than before.
  577.       ((_RopeConcatenation*)__r)->_M_right = __new_right;
  578.       __r->_M_size = __orig_size + __slen;
  579.       if (0 != __r->_M_c_string) {
  580.           __r->_M_free_c_string();
  581.           __r->_M_c_string = 0;
  582.       }
  583.       return __r;
  584.     }
  585.     }
  586.     _RopeRep* __right =
  587.       __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
  588.     __r->_M_ref_nonnil();
  589.     __STL_TRY {
  590.       __result = _S_tree_concat(__r, __right);
  591.     }
  592.     __STL_UNWIND(_S_unref(__r); _S_unref(__right))
  593.     __stl_assert(1 == __result->_M_ref_count);
  594.     return __result;
  595. }
  596. #endif /* !__GC */
  597.  
  598. template <class _CharT, class _Alloc>
  599. rope<_CharT,_Alloc>::_RopeRep*
  600. rope<_CharT,_Alloc>::_S_concat(_RopeRep* __left, _RopeRep* __right)
  601. {
  602.     if (0 == __left) {
  603.     _S_ref(__right);
  604.     return __right;
  605.     }
  606.     if (0 == __right) {
  607.     __left->_M_ref_nonnil();
  608.     return __left;
  609.     }
  610.     if (_RopeRep::_S_leaf == __right->_M_tag) {
  611.     if (_RopeRep::_S_leaf == __left->_M_tag) {
  612.       if (__right->_M_size + __left->_M_size <= _S_copy_max) {
  613.         return _S_leaf_concat_char_iter((_RopeLeaf*)__left,
  614.                      ((_RopeLeaf*)__right)->_M_data,
  615.                      __right->_M_size);
  616.       }
  617.     } else if (_RopeRep::_S_concat == __left->_M_tag
  618.            && _RopeRep::_S_leaf ==
  619.               ((_RopeConcatenation*)__left)->_M_right->_M_tag) {
  620.       _RopeLeaf* __leftright =
  621.             (_RopeLeaf*)(((_RopeConcatenation*)__left)->_M_right); 
  622.       if (__leftright->_M_size + __right->_M_size <= _S_copy_max) {
  623.         _RopeRep* __leftleft = ((_RopeConcatenation*)__left)->_M_left;
  624.         _RopeRep* __rest = _S_leaf_concat_char_iter(__leftright,
  625.                        ((_RopeLeaf*)__right)->_M_data,
  626.                        __right->_M_size);
  627.         __leftleft->_M_ref_nonnil();
  628.         __STL_TRY {
  629.           return(_S_tree_concat(__leftleft, __rest));
  630.             }
  631.         __STL_UNWIND(_S_unref(__leftleft); _S_unref(__rest))
  632.       }
  633.     }
  634.     }
  635.     __left->_M_ref_nonnil();
  636.     __right->_M_ref_nonnil();
  637.     __STL_TRY {
  638.       return(_S_tree_concat(__left, __right));
  639.     }
  640.     __STL_UNWIND(_S_unref(__left); _S_unref(__right));
  641. }
  642.  
  643. template <class _CharT, class _Alloc>
  644. rope<_CharT,_Alloc>::_RopeRep*
  645. rope<_CharT,_Alloc>::_S_substring(_RopeRep* __base, 
  646.                                size_t __start, size_t __endp1)
  647. {
  648.     if (0 == __base) return 0;
  649.     size_t __len = __base->_M_size;
  650.     size_t __adj_endp1;
  651.     const size_t __lazy_threshold = 128;
  652.     
  653.     if (__endp1 >= __len) {
  654.     if (0 == __start) {
  655.         __base->_M_ref_nonnil();
  656.         return __base;
  657.     } else {
  658.         __adj_endp1 = __len;
  659.     }
  660.     } else {
  661.     __adj_endp1 = __endp1;
  662.     }
  663.     switch(__base->_M_tag) {
  664.     case _RopeRep::_S_concat:
  665.         {
  666.         _RopeConcatenation* __c = (_RopeConcatenation*)__base;
  667.         _RopeRep* __left = __c->_M_left;
  668.         _RopeRep* __right = __c->_M_right;
  669.         size_t __left_len = __left->_M_size;
  670.         _RopeRep* __result;
  671.  
  672.         if (__adj_endp1 <= __left_len) {
  673.             return _S_substring(__left, __start, __endp1);
  674.         } else if (__start >= __left_len) {
  675.             return _S_substring(__right, __start - __left_len,
  676.                   __adj_endp1 - __left_len);
  677.         }
  678.         _Self_destruct_ptr __left_result(
  679.           _S_substring(__left, __start, __left_len));
  680.         _Self_destruct_ptr __right_result(
  681.           _S_substring(__right, 0, __endp1 - __left_len));
  682.         __result = _S_concat(__left_result, __right_result);
  683. #               ifndef __GC
  684.           __stl_assert(1 == __result->_M_ref_count);
  685. #               endif
  686.         return __result;
  687.         }
  688.     case _RopeRep::_S_leaf:
  689.         {
  690.         _RopeLeaf* __l = (_RopeLeaf*)__base;
  691.         _RopeLeaf* __result;
  692.         size_t __result_len;
  693.         if (__start >= __adj_endp1) return 0;
  694.         __result_len = __adj_endp1 - __start;
  695.         if (__result_len > __lazy_threshold) goto lazy;
  696. #               ifdef __GC
  697.             const _CharT* __section = __l->_M_data + __start;
  698.             __result = _S_new_RopeLeaf(__section, __result_len,
  699.                       __base->get_allocator());
  700.             __result->_M_c_string = 0;  // Not eos terminated.
  701. #               else
  702.             // We should sometimes create substring node instead.
  703.             __result = __STL_ROPE_FROM_UNOWNED_CHAR_PTR(
  704.                     __l->_M_data + __start, __result_len,
  705.                     __base->get_allocator());
  706. #               endif
  707.         return __result;
  708.         }
  709.     case _RopeRep::_S_substringfn:
  710.         // Avoid introducing multiple layers of substring nodes.
  711.         {
  712.         _RopeSubstring* __old = (_RopeSubstring*)__base;
  713.         size_t __result_len;
  714.         if (__start >= __adj_endp1) return 0;
  715.         __result_len = __adj_endp1 - __start;
  716.         if (__result_len > __lazy_threshold) {
  717.             _RopeSubstring* __result =
  718.             _S_new_RopeSubstring(__old->_M_base,
  719.                       __start + __old->_M_start,
  720.                       __adj_endp1 - __start,
  721.                       __base->get_allocator());
  722.             return __result;
  723.  
  724.         } // *** else fall through: ***
  725.         }
  726.     case _RopeRep::_S_function:
  727.         {
  728.         _RopeFunction* __f = (_RopeFunction*)__base;
  729.         _CharT* __section;
  730.         size_t __result_len;
  731.         if (__start >= __adj_endp1) return 0;
  732.         __result_len = __adj_endp1 - __start;
  733.  
  734.         if (__result_len > __lazy_threshold) goto lazy;
  735.         __section = (_CharT*)
  736.             _Data_allocate(_S_rounded_up_size(__result_len));
  737.         __STL_TRY {
  738.           (*(__f->_M_fn))(__start, __result_len, __section);
  739.                 }
  740.         __STL_UNWIND(_RopeRep::__STL_FREE_STRING(
  741.                    __section, __result_len, __base->get_allocator()));
  742.         _S_cond_store_eos(__section[__result_len]);
  743.         return _S_new_RopeLeaf(__section, __result_len,
  744.                        __base->get_allocator());
  745.         }
  746.     }
  747.     /*NOTREACHED*/
  748.     __stl_assert(false);
  749.   lazy:
  750.     {
  751.     // Create substring node.
  752.     return _S_new_RopeSubstring(__base, __start, __adj_endp1 - __start,
  753.                    __base->get_allocator());
  754.     }
  755. }
  756.  
  757. template<class _CharT>
  758. class _Rope_flatten_char_consumer : public _Rope_char_consumer<_CharT> {
  759.     private:
  760.     _CharT* _M_buf_ptr;
  761.     public:
  762.  
  763.     _Rope_flatten_char_consumer(_CharT* __buffer) {
  764.         _M_buf_ptr = __buffer;
  765.     };
  766.     ~_Rope_flatten_char_consumer() {}
  767.     bool operator() (const _CharT* __leaf, size_t __n) {
  768.         uninitialized_copy_n(__leaf, __n, _M_buf_ptr);
  769.         _M_buf_ptr += __n;
  770.         return true;
  771.     }
  772. };
  773.         
  774. template<class _CharT>
  775. class _Rope_find_char_char_consumer : public _Rope_char_consumer<_CharT> {
  776.     private:
  777.     _CharT _M_pattern;
  778.     public:
  779.     size_t _M_count;  // Number of nonmatching characters
  780.     _Rope_find_char_char_consumer(_CharT __p) 
  781.       : _M_pattern(__p), _M_count(0) {}
  782.     ~_Rope_find_char_char_consumer() {}
  783.     bool operator() (const _CharT* __leaf, size_t __n) {
  784.         size_t __i;
  785.         for (__i = 0; __i < __n; __i++) {
  786.         if (__leaf[__i] == _M_pattern) {
  787.             _M_count += __i; return false;
  788.         }
  789.         }
  790.         _M_count += __n; return true;
  791.     }
  792. };
  793.         
  794. #ifdef __STL_USE_NEW_IOSTREAMS
  795.   template<class _CharT, class _Traits>
  796.   // Here _CharT is both the stream and rope character type.
  797. #else
  798.   template<class _CharT>
  799.   // Here _CharT is the rope character type.  Unlike in the
  800.   // above case, we somewhat handle the case in which it doesn't
  801.   // match the stream character type, i.e. char.
  802. #endif
  803. class _Rope_insert_char_consumer : public _Rope_char_consumer<_CharT> {
  804.     private:
  805. #       ifdef __STL_USE_NEW_IOSTREAMS
  806.       typedef basic_ostream<_CharT,_Traits> _Insert_ostream;
  807. #    else
  808.       typedef ostream _Insert_ostream;
  809. #    endif
  810.     _Insert_ostream& _M_o;
  811.     public:
  812.     _Rope_insert_char_consumer(_Insert_ostream& __writer) 
  813.       : _M_o(__writer) {};
  814.     ~_Rope_insert_char_consumer() { };
  815.         // Caller is presumed to own the ostream
  816.     bool operator() (const _CharT* __leaf, size_t __n);
  817.         // Returns true to continue traversal.
  818. };
  819.         
  820. #ifdef __STL_USE_NEW_IOSTREAMS
  821.   template<class _CharT, class _Traits>
  822.   bool _Rope_insert_char_consumer<_CharT, _Traits>::operator()
  823.                                         (const _CharT* __leaf, size_t __n)
  824.   {
  825.     size_t __i;
  826.     //  We assume that formatting is set up correctly for each element.
  827.     for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
  828.     return true;
  829.   }
  830.  
  831. #else
  832.   template<class _CharT>
  833.   bool _Rope_insert_char_consumer<_CharT>::operator()
  834.                     (const _CharT* __leaf, size_t __n)
  835.   {
  836.     size_t __i;
  837.     //  We assume that formatting is set up correctly for each element.
  838.     for (__i = 0; __i < __n; __i++) _M_o << __leaf[__i];
  839.     return true;
  840.   }
  841.  
  842.  
  843.   __STL_TEMPLATE_NULL
  844.   inline bool _Rope_insert_char_consumer<char>::operator()
  845.                     (const char* __leaf, size_t __n)
  846.   {
  847.     size_t __i;
  848.     for (__i = 0; __i < __n; __i++) _M_o.put(__leaf[__i]);
  849.     return true;
  850.   }
  851. #endif
  852.  
  853. template <class _CharT, class _Alloc>
  854. bool rope<_CharT, _Alloc>::_S_apply_to_pieces(
  855.                 _Rope_char_consumer<_CharT>& __c,
  856.                 const _RopeRep* __r,
  857.                 size_t __begin, size_t __end)
  858. {
  859.     if (0 == __r) return true;
  860.     switch(__r->_M_tag) {
  861.     case _RopeRep::_S_concat:
  862.         {
  863.         _RopeConcatenation* __conc = (_RopeConcatenation*)__r;
  864.         _RopeRep* __left =  __conc->_M_left;
  865.         size_t __left_len = __left->_M_size;
  866.         if (__begin < __left_len) {
  867.             size_t __left_end = min(__left_len, __end);
  868.             if (!_S_apply_to_pieces(__c, __left, __begin, __left_end))
  869.             return false;
  870.         }
  871.         if (__end > __left_len) {
  872.             _RopeRep* __right =  __conc->_M_right;
  873.             size_t __right_start = max(__left_len, __begin);
  874.             if (!_S_apply_to_pieces(__c, __right,
  875.                      __right_start - __left_len,
  876.                      __end - __left_len)) {
  877.             return false;
  878.             }
  879.         }
  880.         }
  881.         return true;
  882.     case _RopeRep::_S_leaf:
  883.         {
  884.         _RopeLeaf* __l = (_RopeLeaf*)__r;
  885.         return __c(__l->_M_data + __begin, __end - __begin);
  886.         }
  887.     case _RopeRep::_S_function:
  888.     case _RopeRep::_S_substringfn:
  889.         {
  890.         _RopeFunction* __f = (_RopeFunction*)__r;
  891.         size_t __len = __end - __begin;
  892.         bool __result;
  893.         _CharT* __buffer =
  894.           (_CharT*)alloc::allocate(__len * sizeof(_CharT));
  895.         __STL_TRY {
  896.           (*(__f->_M_fn))(__begin, __len, __buffer);
  897.           __result = __c(__buffer, __len);
  898.                   alloc::deallocate(__buffer, __len * sizeof(_CharT));
  899.                 }
  900.         __STL_UNWIND((alloc::deallocate(__buffer,
  901.                         __len * sizeof(_CharT))))
  902.         return __result;
  903.         }
  904.     default:
  905.         __stl_assert(false);
  906.         /*NOTREACHED*/
  907.         return false;
  908.     }
  909. }
  910.  
  911. #ifdef __STL_USE_NEW_IOSTREAMS
  912.   template<class _CharT, class _Traits>
  913.   inline void _Rope_fill(basic_ostream<_CharT, _Traits>& __o, size_t __n)
  914. #else
  915.   inline void _Rope_fill(ostream& __o, size_t __n)
  916. #endif
  917. {
  918.     char __f = __o.fill();
  919.     size_t __i;
  920.  
  921.     for (__i = 0; __i < __n; __i++) __o.put(__f);
  922. }
  923.     
  924.  
  925. template <class _CharT> inline bool _Rope_is_simple(_CharT*) { return false; }
  926. inline bool _Rope_is_simple(char*) { return true; }
  927. inline bool _Rope_is_simple(wchar_t*) { return true; }
  928.  
  929. #ifdef __STL_USE_NEW_IOSTREAMS
  930.   template<class _CharT, class _Traits, class _Alloc>
  931.   basic_ostream<_CharT, _Traits>& operator<<
  932.                     (basic_ostream<_CharT, _Traits>& __o,
  933.                      const rope<_CharT, _Alloc>& __r)
  934. #else
  935.   template<class _CharT, class _Alloc>
  936.   ostream& operator<< (ostream& __o, const rope<_CharT, _Alloc>& __r)
  937. #endif
  938. {
  939.     size_t __w = __o.width();
  940.     bool __left = bool(__o.flags() & ios::left);
  941.     size_t __pad_len;
  942.     size_t __rope_len = __r.size();
  943. #   ifdef __STL_USE_NEW_IOSTREAMS
  944.       _Rope_insert_char_consumer<_CharT, _Traits> __c(__o);
  945. #   else
  946.       _Rope_insert_char_consumer<_CharT> __c(__o);
  947. #   endif
  948.     bool __is_simple = _Rope_is_simple((_CharT*)0);
  949.     
  950.     if (__rope_len < __w) {
  951.     __pad_len = __w - __rope_len;
  952.     } else {
  953.     __pad_len = 0;
  954.     }
  955.     if (!__is_simple) __o.width(__w/__rope_len);
  956.     __STL_TRY {
  957.       if (__is_simple && !__left && __pad_len > 0) {
  958.     _Rope_fill(__o, __pad_len);
  959.       }
  960.       __r.apply_to_pieces(0, __r.size(), __c);
  961.       if (__is_simple && __left && __pad_len > 0) {
  962.     _Rope_fill(__o, __pad_len);
  963.       }
  964.       if (!__is_simple)
  965.         __o.width(__w);
  966.     }
  967.     __STL_UNWIND(if (!__is_simple) __o.width(__w))
  968.     return __o;
  969. }
  970.  
  971. template <class _CharT, class _Alloc>
  972. _CharT*
  973. rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r,
  974.                  size_t __start, size_t __len,
  975.                  _CharT* __buffer)
  976. {
  977.     _Rope_flatten_char_consumer<_CharT> __c(__buffer);
  978.     _S_apply_to_pieces(__c, __r, __start, __start + __len);
  979.     return(__buffer + __len);
  980. }
  981.  
  982. template <class _CharT, class _Alloc>
  983. size_t
  984. rope<_CharT,_Alloc>::find(_CharT __pattern, size_t __start) const
  985. {
  986.     _Rope_find_char_char_consumer<_CharT> __c(__pattern);
  987.     _S_apply_to_pieces(__c, _M_tree_ptr, __start, size());
  988.     size_type __result_pos = __start + __c._M_count;
  989. #   ifndef __STL_OLD_ROPE_SEMANTICS
  990.     if (__result_pos == size()) __result_pos = npos;
  991. #   endif
  992.     return __result_pos;
  993. }
  994.  
  995. template <class _CharT, class _Alloc>
  996. _CharT*
  997. rope<_CharT,_Alloc>::_S_flatten(_RopeRep* __r, _CharT* __buffer)
  998. {
  999.     if (0 == __r) return __buffer;
  1000.     switch(__r->_M_tag) {
  1001.     case _RopeRep::_S_concat:
  1002.         {
  1003.         _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1004.         _RopeRep* __left = __c->_M_left;
  1005.         _RopeRep* __right = __c->_M_right;
  1006.         _CharT* __rest = _S_flatten(__left, __buffer);
  1007.         return _S_flatten(__right, __rest);
  1008.         }
  1009.     case _RopeRep::_S_leaf:
  1010.         {
  1011.         _RopeLeaf* __l = (_RopeLeaf*)__r;
  1012.         return copy_n(__l->_M_data, __l->_M_size, __buffer).second;
  1013.         }
  1014.     case _RopeRep::_S_function:
  1015.     case _RopeRep::_S_substringfn:
  1016.         // We dont yet do anything with substring nodes.
  1017.         // This needs to be fixed before ropefiles will work well.
  1018.         {
  1019.         _RopeFunction* __f = (_RopeFunction*)__r;
  1020.         (*(__f->_M_fn))(0, __f->_M_size, __buffer);
  1021.         return __buffer + __f->_M_size;
  1022.         }
  1023.     default:
  1024.         __stl_assert(false);
  1025.         /*NOTREACHED*/
  1026.         return 0;
  1027.     }
  1028. }
  1029.  
  1030.  
  1031. // This needs work for _CharT != char
  1032. template <class _CharT, class _Alloc>
  1033. void
  1034. rope<_CharT,_Alloc>::_S_dump(_RopeRep* __r, int __indent)
  1035. {
  1036.     for (int __i = 0; __i < __indent; __i++) putchar(' ');
  1037.     if (0 == __r) {
  1038.     printf("NULL\n"); return;
  1039.     }
  1040.     if (_RopeRep::_S_concat == __r->_M_tag) {
  1041.     _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1042.     _RopeRep* __left = __c->_M_left;
  1043.     _RopeRep* __right = __c->_M_right;
  1044.  
  1045. #       ifdef __GC
  1046.       printf("Concatenation %p (depth = %d, len = %ld, %s balanced)\n",
  1047.         __r, __r->_M_depth, __r->_M_size, __r->_M_is_balanced? "" : "not");
  1048. #       else
  1049.       printf("Concatenation %p (rc = %ld, depth = %d, "
  1050.                "len = %ld, %s balanced)\n",
  1051.          __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size,
  1052.          __r->_M_is_balanced? "" : "not");
  1053. #       endif
  1054.     _S_dump(__left, __indent + 2);
  1055.     _S_dump(__right, __indent + 2);
  1056.     return;
  1057.     } else {
  1058.     char* __kind;
  1059.  
  1060.     switch (__r->_M_tag) {
  1061.         case _RopeRep::_S_leaf:
  1062.         __kind = "Leaf";
  1063.         break;
  1064.         case _RopeRep::_S_function:
  1065.         __kind = "Function";
  1066.         break;
  1067.         case _RopeRep::_S_substringfn:
  1068.         __kind = "Function representing substring";
  1069.         break;
  1070.         default:
  1071.         __kind = "(corrupted kind field!)";
  1072.     }
  1073. #       ifdef __GC
  1074.       printf("%s %p (depth = %d, len = %ld) ",
  1075.          __kind, __r, __r->_M_depth, __r->_M_size);
  1076. #       else
  1077.       printf("%s %p (rc = %ld, depth = %d, len = %ld) ",
  1078.          __kind, __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size);
  1079. #       endif
  1080.     if (_S_is_one_byte_char_type((_CharT*)0)) {
  1081.         const int __max_len = 40;
  1082.         _Self_destruct_ptr __prefix(_S_substring(__r, 0, __max_len));
  1083.         _CharT __buffer[__max_len + 1];
  1084.         bool __too_big = __r->_M_size > __prefix->_M_size;
  1085.  
  1086.         _S_flatten(__prefix, __buffer);
  1087.         __buffer[__prefix->_M_size] = _S_eos((_CharT*)0); 
  1088.         printf("%s%s\n", 
  1089.                (char*)__buffer, __too_big? "...\n" : "\n");
  1090.     } else {
  1091.         printf("\n");
  1092.     }
  1093.     }
  1094. }
  1095.  
  1096. template <class _CharT, class _Alloc>
  1097. const unsigned long
  1098. rope<_CharT,_Alloc>::_S_min_len[
  1099.   _Rope_RopeRep<_CharT,_Alloc>::_S_max_rope_depth + 1] = {
  1100. /* 0 */1, /* 1 */2, /* 2 */3, /* 3 */5, /* 4 */8, /* 5 */13, /* 6 */21,
  1101. /* 7 */34, /* 8 */55, /* 9 */89, /* 10 */144, /* 11 */233, /* 12 */377,
  1102. /* 13 */610, /* 14 */987, /* 15 */1597, /* 16 */2584, /* 17 */4181,
  1103. /* 18 */6765, /* 19 */10946, /* 20 */17711, /* 21 */28657, /* 22 */46368,
  1104. /* 23 */75025, /* 24 */121393, /* 25 */196418, /* 26 */317811,
  1105. /* 27 */514229, /* 28 */832040, /* 29 */1346269, /* 30 */2178309,
  1106. /* 31 */3524578, /* 32 */5702887, /* 33 */9227465, /* 34 */14930352,
  1107. /* 35 */24157817, /* 36 */39088169, /* 37 */63245986, /* 38 */102334155,
  1108. /* 39 */165580141, /* 40 */267914296, /* 41 */433494437,
  1109. /* 42 */701408733, /* 43 */1134903170, /* 44 */1836311903,
  1110. /* 45 */2971215073u };
  1111. // These are Fibonacci numbers < 2**32.
  1112.  
  1113. template <class _CharT, class _Alloc>
  1114. rope<_CharT,_Alloc>::_RopeRep*
  1115. rope<_CharT,_Alloc>::_S_balance(_RopeRep* __r)
  1116. {
  1117.     _RopeRep* __forest[_RopeRep::_S_max_rope_depth + 1];
  1118.     _RopeRep* __result = 0;
  1119.     int __i;
  1120.     // Invariant:
  1121.     // The concatenation of forest in descending order is equal to __r.
  1122.     // __forest[__i]._M_size >= _S_min_len[__i]
  1123.     // __forest[__i]._M_depth = __i
  1124.     // References from forest are included in refcount.
  1125.  
  1126.     for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i) 
  1127.       __forest[__i] = 0;
  1128.     __STL_TRY {
  1129.       _S_add_to_forest(__r, __forest);
  1130.       for (__i = 0; __i <= _RopeRep::_S_max_rope_depth; ++__i) 
  1131.         if (0 != __forest[__i]) {
  1132. #    ifndef __GC
  1133.       _Self_destruct_ptr __old(__result);
  1134. #    endif
  1135.       __result = _S_concat(__forest[__i], __result);
  1136.     __forest[__i]->_M_unref_nonnil();
  1137. #    if !defined(__GC) && defined(__STL_USE_EXCEPTIONS)
  1138.       __forest[__i] = 0;
  1139. #    endif
  1140.       }
  1141.     }
  1142.     __STL_UNWIND(for(__i = 0; __i <= _RopeRep::_S_max_rope_depth; __i++)
  1143.          _S_unref(__forest[__i]))
  1144.     if (__result->_M_depth > _RopeRep::_S_max_rope_depth) {
  1145. #     ifdef __STL_USE_EXCEPTIONS
  1146.     __STL_THROW(length_error("rope too long"));
  1147. #     else
  1148.     abort();
  1149. #     endif
  1150.     }
  1151.     return(__result);
  1152. }
  1153.  
  1154.  
  1155. template <class _CharT, class _Alloc>
  1156. void
  1157. rope<_CharT,_Alloc>::_S_add_to_forest(_RopeRep* __r, _RopeRep** __forest)
  1158. {
  1159.     if (__r->_M_is_balanced) {
  1160.     _S_add_leaf_to_forest(__r, __forest);
  1161.     return;
  1162.     }
  1163.     __stl_assert(__r->_M_tag == _RopeRep::_S_concat);
  1164.     {
  1165.     _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1166.  
  1167.     _S_add_to_forest(__c->_M_left, __forest);
  1168.     _S_add_to_forest(__c->_M_right, __forest);
  1169.     }
  1170. }
  1171.  
  1172.  
  1173. template <class _CharT, class _Alloc>
  1174. void
  1175. rope<_CharT,_Alloc>::_S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest)
  1176. {
  1177.     _RopeRep* __insertee;           // included in refcount
  1178.     _RopeRep* __too_tiny = 0;        // included in refcount
  1179.     int __i;                  // forest[0..__i-1] is empty
  1180.     size_t __s = __r->_M_size;
  1181.  
  1182.     for (__i = 0; __s >= _S_min_len[__i+1]/* not this bucket */; ++__i) {
  1183.     if (0 != __forest[__i]) {
  1184. #        ifndef __GC
  1185.           _Self_destruct_ptr __old(__too_tiny);
  1186. #        endif
  1187.         __too_tiny = _S_concat_and_set_balanced(__forest[__i], __too_tiny);
  1188.         __forest[__i]->_M_unref_nonnil();
  1189.         __forest[__i] = 0;
  1190.     }
  1191.     }
  1192.     {
  1193. #    ifndef __GC
  1194.       _Self_destruct_ptr __old(__too_tiny);
  1195. #    endif
  1196.     __insertee = _S_concat_and_set_balanced(__too_tiny, __r);
  1197.     }
  1198.     // Too_tiny dead, and no longer included in refcount.
  1199.     // Insertee is live and included.
  1200.     __stl_assert(_S_is_almost_balanced(__insertee));
  1201.     __stl_assert(__insertee->_M_depth <= __r->_M_depth + 1);
  1202.     for (;; ++__i) {
  1203.     if (0 != __forest[__i]) {
  1204. #        ifndef __GC
  1205.           _Self_destruct_ptr __old(__insertee);
  1206. #        endif
  1207.         __insertee = _S_concat_and_set_balanced(__forest[__i], __insertee);
  1208.         __forest[__i]->_M_unref_nonnil();
  1209.         __forest[__i] = 0;
  1210.         __stl_assert(_S_is_almost_balanced(__insertee));
  1211.     }
  1212.     __stl_assert(_S_min_len[__i] <= __insertee->_M_size);
  1213.     __stl_assert(__forest[__i] == 0);
  1214.     if (__i == _RopeRep::_S_max_rope_depth || 
  1215.           __insertee->_M_size < _S_min_len[__i+1]) {
  1216.         __forest[__i] = __insertee;
  1217.         // refcount is OK since __insertee is now dead.
  1218.         return;
  1219.     }
  1220.     }
  1221. }
  1222.  
  1223. template <class _CharT, class _Alloc>
  1224. _CharT
  1225. rope<_CharT,_Alloc>::_S_fetch(_RopeRep* __r, size_type __i)
  1226. {
  1227.     __GC_CONST _CharT* __cstr = __r->_M_c_string;
  1228.  
  1229.     __stl_assert(__i < __r->_M_size);
  1230.     if (0 != __cstr) return __cstr[__i]; 
  1231.     for(;;) {
  1232.       switch(__r->_M_tag) {
  1233.     case _RopeRep::_S_concat:
  1234.         {
  1235.         _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1236.         _RopeRep* __left = __c->_M_left;
  1237.         size_t __left_len = __left->_M_size;
  1238.  
  1239.         if (__i >= __left_len) {
  1240.             __i -= __left_len;
  1241.             __r = __c->_M_right;
  1242.         } else {
  1243.             __r = __left;
  1244.         }
  1245.         }
  1246.         break;
  1247.     case _RopeRep::_S_leaf:
  1248.         {
  1249.         _RopeLeaf* __l = (_RopeLeaf*)__r;
  1250.         return __l->_M_data[__i];
  1251.         }
  1252.     case _RopeRep::_S_function:
  1253.     case _RopeRep::_S_substringfn:
  1254.         {
  1255.         _RopeFunction* __f = (_RopeFunction*)__r;
  1256.         _CharT __result;
  1257.  
  1258.         (*(__f->_M_fn))(__i, 1, &__result);
  1259.         return __result;
  1260.         }
  1261.       }
  1262.     }
  1263. }
  1264.  
  1265. # ifndef __GC
  1266. // Return a uniquely referenced character slot for the given
  1267. // position, or 0 if that's not possible.
  1268. template <class _CharT, class _Alloc>
  1269. _CharT*
  1270. rope<_CharT,_Alloc>::_S_fetch_ptr(_RopeRep* __r, size_type __i)
  1271. {
  1272.     _RopeRep* __clrstack[_RopeRep::_S_max_rope_depth];
  1273.     size_t __csptr = 0;
  1274.  
  1275.     for(;;) {
  1276.       if (__r->_M_ref_count > 1) return 0;
  1277.       switch(__r->_M_tag) {
  1278.     case _RopeRep::_S_concat:
  1279.         {
  1280.         _RopeConcatenation* __c = (_RopeConcatenation*)__r;
  1281.         _RopeRep* __left = __c->_M_left;
  1282.         size_t __left_len = __left->_M_size;
  1283.  
  1284.         if (__c->_M_c_string != 0) __clrstack[__csptr++] = __c;
  1285.         if (__i >= __left_len) {
  1286.             __i -= __left_len;
  1287.             __r = __c->_M_right;
  1288.         } else {
  1289.             __r = __left;
  1290.         }
  1291.         }
  1292.         break;
  1293.     case _RopeRep::_S_leaf:
  1294.         {
  1295.         _RopeLeaf* __l = (_RopeLeaf*)__r;
  1296.         if (__l->_M_c_string != __l->_M_data && __l->_M_c_string != 0)
  1297.             __clrstack[__csptr++] = __l;
  1298.         while (__csptr > 0) {
  1299.             -- __csptr;
  1300.             _RopeRep* __d = __clrstack[__csptr];
  1301.             __d->_M_free_c_string();
  1302.             __d->_M_c_string = 0;
  1303.         }
  1304.         return __l->_M_data + __i;
  1305.         }
  1306.     case _RopeRep::_S_function:
  1307.     case _RopeRep::_S_substringfn:
  1308.         return 0;
  1309.       }
  1310.     }
  1311. }
  1312. # endif /* __GC */
  1313.  
  1314. // The following could be implemented trivially using
  1315. // lexicographical_compare_3way.
  1316. // We do a little more work to avoid dealing with rope iterators for
  1317. // flat strings.
  1318. template <class _CharT, class _Alloc>
  1319. int
  1320. rope<_CharT,_Alloc>::_S_compare (const _RopeRep* __left, 
  1321.                                  const _RopeRep* __right)
  1322. {
  1323.     size_t __left_len;
  1324.     size_t __right_len;
  1325.  
  1326.     if (0 == __right) return 0 != __left;
  1327.     if (0 == __left) return -1;
  1328.     __left_len = __left->_M_size;
  1329.     __right_len = __right->_M_size;
  1330.     if (_RopeRep::_S_leaf == __left->_M_tag) {
  1331.     _RopeLeaf* __l = (_RopeLeaf*) __left;
  1332.     if (_RopeRep::_S_leaf == __right->_M_tag) {
  1333.         _RopeLeaf* __r = (_RopeLeaf*) __right;
  1334.         return lexicographical_compare_3way(
  1335.             __l->_M_data, __l->_M_data + __left_len,
  1336.             __r->_M_data, __r->_M_data + __right_len);
  1337.     } else {
  1338.         const_iterator __rstart(__right, 0);
  1339.         const_iterator __rend(__right, __right_len);
  1340.         return lexicographical_compare_3way(
  1341.             __l->_M_data, __l->_M_data + __left_len,
  1342.             __rstart, __rend);
  1343.     }
  1344.     } else {
  1345.     const_iterator __lstart(__left, 0);
  1346.     const_iterator __lend(__left, __left_len);
  1347.     if (_RopeRep::_S_leaf == __right->_M_tag) {
  1348.         _RopeLeaf* __r = (_RopeLeaf*) __right;
  1349.         return lexicographical_compare_3way(
  1350.                    __lstart, __lend,
  1351.                    __r->_M_data, __r->_M_data + __right_len);
  1352.     } else {
  1353.         const_iterator __rstart(__right, 0);
  1354.         const_iterator __rend(__right, __right_len);
  1355.         return lexicographical_compare_3way(
  1356.                    __lstart, __lend,
  1357.                    __rstart, __rend);
  1358.     }
  1359.     }
  1360. }
  1361.  
  1362. // Assignment to reference proxies.
  1363. template <class _CharT, class _Alloc>
  1364. _Rope_char_ref_proxy<_CharT, _Alloc>&
  1365. _Rope_char_ref_proxy<_CharT, _Alloc>::operator= (_CharT __c) {
  1366.     _RopeRep* __old = _M_root->_M_tree_ptr;
  1367. #   ifndef __GC
  1368.     // First check for the case in which everything is uniquely
  1369.     // referenced.  In that case we can do this destructively.
  1370.     _CharT* __ptr = _My_rope::_S_fetch_ptr(__old, _M_pos);
  1371.     if (0 != __ptr) {
  1372.         *__ptr = __c;
  1373.         return *this;
  1374.     }
  1375. #   endif
  1376.     _Self_destruct_ptr __left(
  1377.       _My_rope::_S_substring(__old, 0, _M_pos));
  1378.     _Self_destruct_ptr __right(
  1379.       _My_rope::_S_substring(__old, _M_pos+1, __old->_M_size));
  1380.     _Self_destruct_ptr __result_left(
  1381.       _My_rope::_S_destr_concat_char_iter(__left, &__c, 1));
  1382.  
  1383. #   ifndef __GC
  1384.       __stl_assert(__left == __result_left || 1 == __result_left->_M_ref_count);
  1385. #   endif
  1386.     _RopeRep* __result =
  1387.         _My_rope::_S_concat(__result_left, __right);
  1388. #   ifndef __GC
  1389.       __stl_assert(1 <= __result->_M_ref_count);
  1390.       _RopeRep::_S_unref(__old);
  1391. #   endif
  1392.     _M_root->_M_tree_ptr = __result;
  1393.     return *this;
  1394. }
  1395.  
  1396. template <class _CharT, class _Alloc>
  1397. inline _Rope_char_ref_proxy<_CharT, _Alloc>::operator _CharT () const
  1398. {
  1399.     if (_M_current_valid) {
  1400.     return _M_current;
  1401.     } else {
  1402.         return _My_rope::_S_fetch(_M_root->_M_tree_ptr, _M_pos);
  1403.     }
  1404. }
  1405. template <class _CharT, class _Alloc>
  1406. _Rope_char_ptr_proxy<_CharT, _Alloc>
  1407. _Rope_char_ref_proxy<_CharT, _Alloc>::operator& () const {
  1408.     return _Rope_char_ptr_proxy<_CharT, _Alloc>(*this);
  1409. }
  1410.  
  1411. template <class _CharT, class _Alloc>
  1412. rope<_CharT, _Alloc>::rope(size_t __n, _CharT __c,
  1413.                const allocator_type& __a)
  1414. : _Base(__a)
  1415. {
  1416.     rope<_CharT,_Alloc> __result;
  1417.     const size_t __exponentiate_threshold = 32;
  1418.     size_t __exponent;
  1419.     size_t __rest;
  1420.     _CharT* __rest_buffer;
  1421.     _RopeRep* __remainder;
  1422.     rope<_CharT,_Alloc> __remainder_rope;
  1423.  
  1424.     if (0 == __n)
  1425.       return;
  1426.     
  1427.     __exponent = __n / __exponentiate_threshold;
  1428.     __rest = __n % __exponentiate_threshold;
  1429.     if (0 == __rest) {
  1430.     __remainder = 0;
  1431.     } else {
  1432.     __rest_buffer = _Data_allocate(_S_rounded_up_size(__rest));
  1433.     uninitialized_fill_n(__rest_buffer, __rest, __c);
  1434.     _S_cond_store_eos(__rest_buffer[__rest]);
  1435.     __STL_TRY {
  1436.         __remainder = _S_new_RopeLeaf(__rest_buffer, __rest, __a);
  1437.         }
  1438.     __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__rest_buffer, __rest, __a))
  1439.     }
  1440.     __remainder_rope._M_tree_ptr = __remainder;
  1441.     if (__exponent != 0) {
  1442.     _CharT* __base_buffer =
  1443.       _Data_allocate(_S_rounded_up_size(__exponentiate_threshold));
  1444.     _RopeLeaf* __base_leaf;
  1445.     rope __base_rope;
  1446.     uninitialized_fill_n(__base_buffer, __exponentiate_threshold, __c);
  1447.     _S_cond_store_eos(__base_buffer[__exponentiate_threshold]);
  1448.     __STL_TRY {
  1449.           __base_leaf = _S_new_RopeLeaf(__base_buffer,
  1450.                                         __exponentiate_threshold, __a);
  1451.         }
  1452.     __STL_UNWIND(_RopeRep::__STL_FREE_STRING(__base_buffer, 
  1453.                                              __exponentiate_threshold, __a))
  1454.     __base_rope._M_tree_ptr = __base_leaf;
  1455.      if (1 == __exponent) {
  1456.       __result = __base_rope;
  1457. #         ifndef __GC
  1458.         __stl_assert(2 == __result._M_tree_ptr->_M_ref_count);
  1459.         // One each for base_rope and __result
  1460. #         endif
  1461.     } else {
  1462.       __result = power(__base_rope, __exponent,
  1463.                _Rope_Concat_fn<_CharT,_Alloc>());
  1464.     }
  1465.     if (0 != __remainder) {
  1466.       __result += __remainder_rope;
  1467.     }
  1468.     } else {
  1469.     __result = __remainder_rope;
  1470.     }
  1471.     _M_tree_ptr = __result._M_tree_ptr;
  1472.     _M_tree_ptr->_M_ref_nonnil();
  1473. }
  1474.  
  1475. template<class _CharT, class _Alloc>
  1476.   _CharT rope<_CharT,_Alloc>::_S_empty_c_str[1];
  1477.  
  1478. template<class _CharT, class _Alloc>
  1479. const _CharT* rope<_CharT,_Alloc>::c_str() const {
  1480.     if (0 == _M_tree_ptr) {
  1481.         _S_empty_c_str[0] = _S_eos((_CharT*)0);  // Possibly redundant,
  1482.                          // but probably fast.
  1483.         return _S_empty_c_str;
  1484.     }
  1485.     __GC_CONST _CharT* __old_c_string = _M_tree_ptr->_M_c_string;
  1486.     if (0 != __old_c_string) return(__old_c_string);
  1487.     size_t __s = size();
  1488.     _CharT* __result = _Data_allocate(__s + 1);
  1489.     _S_flatten(_M_tree_ptr, __result);
  1490.     __result[__s] = _S_eos((_CharT*)0);
  1491. #   ifdef __GC
  1492.     _M_tree_ptr->_M_c_string = __result;
  1493. #   else
  1494.       if ((__old_c_string = (__GC_CONST _CharT*)
  1495.              _Atomic_swap((unsigned long *)(&(_M_tree_ptr->_M_c_string)),
  1496.               (unsigned long)__result)) != 0) {
  1497.     // It must have been added in the interim.  Hence it had to have been
  1498.     // separately allocated.  Deallocate the old copy, since we just
  1499.     // replaced it.
  1500.     destroy(__old_c_string, __old_c_string + __s + 1);
  1501.     _Data_deallocate(__old_c_string, __s + 1);
  1502.       }
  1503. #   endif
  1504.     return(__result);
  1505. }
  1506.  
  1507. template<class _CharT, class _Alloc>
  1508. const _CharT* rope<_CharT,_Alloc>::replace_with_c_str() {
  1509.     if (0 == _M_tree_ptr) {
  1510.         _S_empty_c_str[0] = _S_eos((_CharT*)0);
  1511.         return _S_empty_c_str;
  1512.     }
  1513.     __GC_CONST _CharT* __old_c_string = _M_tree_ptr->_M_c_string;
  1514.     if (_RopeRep::_S_leaf == _M_tree_ptr->_M_tag && 0 != __old_c_string) {
  1515.     return(__old_c_string);
  1516.     }
  1517.     size_t __s = size();
  1518.     _CharT* __result = _Data_allocate(_S_rounded_up_size(__s));
  1519.     _S_flatten(_M_tree_ptr, __result);
  1520.     __result[__s] = _S_eos((_CharT*)0);
  1521.     _M_tree_ptr->_M_unref_nonnil();
  1522.     _M_tree_ptr = _S_new_RopeLeaf(__result, __s, get_allocator());
  1523.     return(__result);
  1524. }
  1525.  
  1526. // Algorithm specializations.  More should be added.
  1527.  
  1528. template<class _Rope_iterator>  // was templated on CharT and Alloc
  1529. void                // VC++ workaround
  1530. _Rope_rotate(_Rope_iterator __first,
  1531.              _Rope_iterator __middle,
  1532.              _Rope_iterator __last)
  1533. {
  1534.   typedef typename _Rope_iterator::value_type _CharT;
  1535.   typedef typename _Rope_iterator::_allocator_type _Alloc;
  1536.   
  1537.   __stl_assert(__first.container() == __middle.container()
  1538.                            && __middle.container() == __last.container());
  1539.   rope<_CharT,_Alloc>& __r(__first.container());
  1540.   rope<_CharT,_Alloc> __prefix = __r.substr(0, __first.index());
  1541.   rope<_CharT,_Alloc> __suffix = 
  1542.     __r.substr(__last.index(), __r.size() - __last.index());
  1543.   rope<_CharT,_Alloc> __part1 = 
  1544.     __r.substr(__middle.index(), __last.index() - __middle.index());
  1545.   rope<_CharT,_Alloc> __part2 = 
  1546.     __r.substr(__first.index(), __middle.index() - __first.index());
  1547.   __r = __prefix;
  1548.   __r += __part1;
  1549.   __r += __part2;
  1550.   __r += __suffix;
  1551. }
  1552.  
  1553. #if !defined(__GNUC__)
  1554. // Appears to confuse g++
  1555. inline void rotate(_Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __first,
  1556.                    _Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __middle,
  1557.                    _Rope_iterator<char,__STL_DEFAULT_ALLOCATOR(char)> __last) {
  1558.     _Rope_rotate(__first, __middle, __last);
  1559. }
  1560. #endif
  1561.  
  1562. # if 0
  1563. // Probably not useful for several reasons:
  1564. // - for SGIs 7.1 compiler and probably some others,
  1565. //   this forces lots of rope<wchar_t, ...> instantiations, creating a
  1566. //   code bloat and compile time problem.  (Fixed in 7.2.)
  1567. // - wchar_t is 4 bytes wide on most UNIX platforms, making it unattractive
  1568. //   for unicode strings.  Unsigned short may be a better character
  1569. //   type.
  1570. inline void rotate(
  1571.         _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __first,
  1572.                 _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __middle,
  1573.                 _Rope_iterator<wchar_t,__STL_DEFAULT_ALLOCATOR(char)> __last) {
  1574.     _Rope_rotate(__first, __middle, __last);
  1575. }
  1576. # endif
  1577.  
  1578.  
  1579. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  1580. #pragma reset woff 1174
  1581. #endif
  1582.  
  1583. __STL_END_NAMESPACE
  1584.  
  1585. // Local Variables:
  1586. // mode:C++
  1587. // End:
  1588.